package sisi.corporation;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * CorporationController.java - Controller class for the <i>corporation database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 03/10/2013
 * 
 */

public class CorporationController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Corporation[] getCorporation(String cerca) {
		List<Corporation> corporations = getCorporationList(cerca);
		return corporations.toArray(new Corporation[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Corporation> getCorporationList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.corpname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Corporation as c " + cWhere
							+ " order by c.corpname");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Corporation> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	public Corporation getCorporationxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Corporation corporationx = em.find(Corporation.class, id);
			return corporationx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Corporation addCorporation(Corporation corporation) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(corporation);
			em.getTransaction().commit();
			corporation = refreshCorporation(corporation);
		} finally {
			em.close();
			return corporation;
		}
	}

	@SuppressWarnings("finally")
	public boolean updateCorporation(Corporation corporation) {

		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Corporation corporationx = em.find(Corporation.class, corporation.getCorpid());
			corporationx.setCorpid(corporation.getCorpid());
			corporationx.setAddress(corporation.getAddress());
			corporationx.setCity(corporation.getCity());
			corporationx.setContactname(corporation.getContactname());
			corporationx.setContacttitle(corporation.getContacttitle());
			corporationx.setCorpname(corporation.getCorpname());
			corporationx.setCountry(corporation.getCountry());
			corporationx.setEmail(corporation.getEmail());
			corporationx.setFax(corporation.getFax());
			corporationx.setGsm(corporation.getGsm());
			corporationx.setPhone(corporation.getPhone());
			corporationx.setPostalcode(corporation.getPostalcode());
			corporationx.setRegion(corporation.getRegion());		
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Corporation refreshCorporation(Corporation corporation) {

		Corporation corporationx = corporation;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			corporationx = em.find(Corporation.class, corporation.getCorpid());
			em.flush();
			em.refresh(corporationx);
		} finally {
			em.close();
			return corporationx;
		}
	}

	@SuppressWarnings("finally")
	public boolean removeCorporation(Corporation corporation) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Corporation corporationx = em.find(Corporation.class, corporation.getCorpid());
			em.remove(corporationx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
